<?php

namespace App\Repositories\Api;

use App\Models\Member;
use App\Models\Enlist;
use App\Models\Config;
use App\Models\Superpick;
use Exception;

class EnlistRepository
{

    public function updateFields($id, $data = [])
    {
        if (intval($id)) {
            return Enlist::where("id", "=", $id)->update($data);
        } else {
            return Enlist::create($data);
        }
    }

    public function getOne(int $id)
    {
        if (!intval($id)) return [];
        $res = Enlist::where("id", "=", $id)->first();
        $res['is_apply'] = Enlist::where("original_enlist_id", "=", $res['id'])->count("id");
        return $res;
    }

    public function getLists(array $filter = [], int $pageSize = 10, int $pageNum = 1)
    {
        $wechat_admin_id = request()->get("wechat_admin_id");
        $res = Enlist::query()
            ->when(isset($filter['_string']), function ($q) use ($filter) {
                return $q->whereRaw($filter['_string']);
            })
            ->when(isset($filter['status']), function ($q) use ($filter) {
                if (is_array($filter['status'])) {
                    return $q->whereIn('status', $filter['status']);
                } else {
                    if ($filter['status'] == 4) {
                        return $q->whereIn("status", [1, 2]);
                    } else {
                        return $q->where("status", "=", $filter['status']);
                    }
                }
            })
            ->when(!empty($filter['enlist_uid']), function ($q) use ($filter) {
                return $q->where("enlist_uid", "=", $filter['enlist_uid']);
            })
            ->when(isset($filter['enlisted_uid']), function ($q) use ($filter) {
                return $q->where("enlisted_uid", "=", $filter['enlisted_uid']);
            })
            ->orderBy('id', 'desc')
            ->paginate($pageSize, "*", null, $pageNum)
            ->toArray();

        $res = filterPaginateData($res);

        foreach ($res['items'] as $k => $v) {
            //应征
            $v['enlisted_uid'] != UID && $v['is_enlisted'] = 1;
            // 被应征
            $v['enlist_uid'] != UID && $v['is_enlisted'] = 2;

            $enlist_uid = $v['is_enlisted'] == 1 ? $v['enlisted_uid'] : $v['enlist_uid'];
            $v['is_super'] = Superpick::where(['uid' => UID, 'df_uid' => $enlist_uid])->count("id");
            $v['member'] = (new MemberRepository())->getSimpleMember($enlist_uid, 3, $v['is_super'] ?? 0);

            //应征通过时间减应征时间超过48*3600-20就为自动通过时间。
            if (strtotime($v['enlist_time']) - strtotime($v['create_time']) >= 7 * 24 * 3600) {
                $v['is_auto_pass'] = 1;
            }

            //剩余处理时间
            // $v['countdowntime'] = strtotime($v['create_time'])+3600*7*24-time();
            $v['countdowntime'] = time2string(strtotime($v['create_time']) + 3600 * 7 * 24 - time());

            $res['items'][$k] = $v;
        }

        return $res;
    }

    public function delete(int $id)
    {
        return Enlist::where("id", "=", $id)->update(['is_delete' => 0]);
    }

    //获取附件的人被应征价格
    function get_enlisted_float_price($uid = 0)
    {
        if (!$uid) return '';

        $num = Enlist::where(['from' => "nearby", 'enlisted_uid' => $uid])
            ->where("create_time", ">", date('Y-m-d', strtotime('-15 days')))
            ->count("id");
        $price = intval(Config::getValue("nearby_price")) + intval(get_float_price($num));

        $member = Member::where("id", "=", request()->get("wechat_admin_id"))->first();
        $enlist_member = Member::where("id", "=", $uid)->first();

        //2.我头像不符合要求 加2
        if ($member['avatar_status'] == 2) {
            $price = $price + 2;
        }

        //3.对方审核通过 加4
        if ($enlist_member['status'] == 1) {
            $price = $price + 4;
        }
        return $price;
    }

    /**
     * 超级喜欢
     * @param array $member
     * @param array $e_member
     * @return int
     */
    public function addSuper($df_uid = null, $super_price = 0.00)
    {
        if (empty($df_uid) || !$super_price)
            retFail("数据异常");

        $memberRep = new MemberRepository();
        $df_member = $memberRep->getSimpleMember($df_uid, 3, true);
        $member = $memberRep->getSimpleMember(UID, 3);
        if (empty($df_member))
            return retFail("嘉宾不存在");

        if (empty($df_member['weixin_qrcode_pic']) && empty($df_member['weixin']))
            return retFail("嘉宾微信异常");

        if (Superpick::where(['uid' => UID, 'df_uid' => $df_uid])->count("id"))
            return retFail("已超级喜欢，请不要重复！");

        if ($member['total_account'] < $super_price)
            return retFail("", 413, ['errCode' => 1001, 'errMsg' => "爱心不足"]);

        $res = Superpick::create([
            'uid' => UID,
            'df_uid' => $df_uid
        ]);

        $memberRep->change_user_account(UID, $df_uid, null, 61, $super_price, null, 0);

        return retSuc([
            'id' => $res['id'],
            'wx_qrcode_path' => $df_member['weixin_qrcode_pic'],
            'weixin' => $df_member['weixin']
        ]);
    }

    //根据审核状态获取应征价格
    function get_enlist_price($member = [], $e_member = [])
    {
        $price = 8;
        if ($member['status'] == 3) {
            //我审核不通过 加4
            return $price + 4;
        }
        if ($e_member['status'] == 1) {
            //对方审核通过 加4
            return $price + 4;
        }
        return $price;
        //公众号应征前十名
        // $count = Enlist::where(['enlisted_uid' => $uid])
        //             ->where("from", "<>", "nearby")
        //             ->where("create_time", ">", date('Y-m-d',strtotime('-90 days')))
        //             ->count("id");

        // if ($count>10) {
        //     if ($member['status']==3) {
        //         return 10;
        //     }
        //     return $price;
        // }

        // if ($member['credit'] != 100) {
        //     return $price ;
        // }
        // switch ($member['status']) {
        //     case 0://未提交审核
        //         return $price;
        //         break;
        //     case 1://审核通过
        //         return 0;
        //         break;
        //     case 2://须重新提交审核
        //         return $price;
        //         break;
        //     case 3://审核不通过
        //         return 10;
        //         break;
        //     case 4://已提交待审核
        //         return 0;
        //         break;
        //     default:
        //         return $price;
        //         break;
        // }
    }

}